package hip.util.file;

import hip.util.OSUtil;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileOperate {

    private static final int BUFFEREDSIZE = 1024 * 1024;

    // public static List<String> list= new ArrayList<String>();

    /**
     * 转化文件为字节流
     *
     * @param file
     * @return
     * @throws Exception
     */
    public static byte[] File2ByteStream(File file) throws Exception {
        if (file.isDirectory()) return null;

        BufferedInputStream in = null;
        ByteArrayOutputStream out = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            in = new BufferedInputStream(fis);
            out = new ByteArrayOutputStream();
            byte[] temp = new byte[BUFFEREDSIZE];
            int size = 0;
            while ((size = in.read(temp)) != -1) {
                out.write(temp, 0, size);
            }
            out.flush();
        } finally {
            fis.close();
            in.close();
            out.close();
        }
        return out.toByteArray();
    }

    /**
     * 转化字节流转为文件
     *
     * @param by
     * @param file
     * @throws Exception
     */
    public static void ByteStream2File(byte[] by, File file) throws Exception {
        // if (!file.exists())
        createFolder(file);
        if (by != null) {
            ByteArrayInputStream in = null;
            BufferedOutputStream out = null;
            FileOutputStream fos = null;
            try {

                fos = new FileOutputStream(file);
                in = new ByteArrayInputStream(by);
                out = new BufferedOutputStream(fos);
                byte[] buff = new byte[BUFFEREDSIZE];
                int size = 0;
                while ((size = in.read(buff)) != -1) {
                    out.write(buff, 0, size);
                }
                out.flush();
            } finally {
                if (fos != null) fos.close();
                if (in != null) in.close();
                if (out != null) out.close();
            }
        }
    }

    /**
     * 通过文件夹读取文件，传入文件名、文件日期、文件大小
     */
    public static Map<String, Object> readFile(String path) {
        File myFile = new File(path);
        Map<String, Object> fMap = new HashMap<String, Object>();
        for (int i = 0; i < scanFile(myFile).size(); i++) {
            File file = (File) scanFile(myFile).get(i);
            String fName = file.getName();
            String fDate = String.valueOf(file.lastModified());
            String fLength = String.valueOf(file.length());
            fMap.put(fName, fDate + fLength);
        }
        return fMap;
    }

    /**
     * 通过文件夹读取文件，传入文件名、文件MD5摘要
     */
    public static Map<String, Object> readFlagFile(File absolute, String root) throws FileNotFoundException, IOException {
        Map<String, Object> fMap = new HashMap<String, Object>();
        List<File> fileList = scanFile(absolute);
        for (int i = 0; i < fileList.size(); i++) {
            File file = fileList.get(i);
            if (root.length() >= file.getAbsolutePath().length())// 跟目录
                fMap.put("", "");
            else {
                String fName = file.getAbsolutePath().substring((root + File.separator).length());
                if (file.isFile()) {
                    String fFlag = getMD5Flag(file);
                    fMap.put(fName, fFlag);
                } else {
                    fMap.put(fName, "");
                }
            }
        }
        return fMap;
    }

    public static String getMD5Flag(File file) throws FileNotFoundException, IOException {
        FileInputStream in = new FileInputStream(file);
        String md5 = "";
        try {
            md5 = DigestUtils.md5Hex(in);
        } catch (Exception e) {
        } finally {
            in.close();
        }
        return md5;
    }

    /**
     * 递归删除文件
     *
     * @param file
     */
    public static void deleteFile(File file) {
        if (file.exists()) file.delete();
    }

    /**
     * 删除文件夹
     *
     * @param file
     * @return
     */
    public static void deleteFolder(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                deleteFolder(f);
            }
        }
        file.delete();

    }

    /**
     * 获取文件夹下所有的文件夹名
     *
     * @param file
     * @return
     */
    public static List<String> getDirectory(File file) {
        List<String> list = new ArrayList<String>();
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                getDirectory(f, list);
            }
        }
        return list;
    }

    private static void getDirectory(File file, List<String> list) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                getDirectory(f, list);
            }
            list.add(file.getAbsolutePath());
        }
    }

    /**
     * 建立文件夹
     *
     */
    public static void createFolder(File file) {
        String separator = "";
        if (OSUtil.getOsType() == OSUtil.OSType.WINDWODS) {
            separator = "\\\\";
        } else if (OSUtil.getOsType() == OSUtil.OSType.LIUNX) {
            separator = "/";
        }
        String folders[] = file.getAbsolutePath().split(separator);
        String p = "";
        for (int i = 0; i < folders.length; i++) {
            if (p != "") {
                p = p + File.separator + folders[i] + File.separator;
            } else {
                p += folders[i] + File.separator;
            }
            File newfile = new File(p);
            if (!newfile.exists()) {
                if (!FileOperate.isFile(file) || i < folders.length - 1) newfile.mkdirs();
            }
        }
    }

    // 搜索文件夹下的文件，返回文件列表
    public static List<File> scanFile(File root) {
        List<File> fileInfo = new ArrayList<File>();
        if (root.exists()) {
            if (root.isFile())// loanin 如果是一个文件，则直接返回该文件
                fileInfo.add(root);
            else {
                fileInfo.add(root);
                File[] files = root.listFiles();
                if (files != null) {
                    for (File file : files) {// 逐个遍历文件
                        if (file.isDirectory()) { // 如果是文件夹，则进一步遍历，递归调用
                            List<File> ff = scanFile(file);
                            fileInfo.addAll(ff);
                            fileInfo.add(file);
                        } else {
                            fileInfo.add(file);
                        }
                    }
                }
                // else {
                // fileInfo.add(root);
                // }
            }
        }
        return fileInfo;
    }

    /**
     * 复制文件夹
     *
     * @param sourceDir
     * @param targetDir
     * @throws IOException
     */
    public static void copyDirectiory(String sourceDir, String targetDir) throws IOException {
        if (!new File(sourceDir).exists()) return;
        createFolder(new File(targetDir));
        File[] file = (new File(sourceDir)).listFiles();
        for (int i = 0; i < file.length; i++) {
            if (file[i].isFile()) {
                File sourceFile = file[i];
                File targetFile = new File(new File(targetDir).getAbsolutePath() + File.separator + file[i].getName());
                copyFile(sourceFile, targetFile);
            }
            if (file[i].isDirectory()) {
                String dir1 = sourceDir + File.separator + file[i].getName();
                String dir2 = targetDir + File.separator + file[i].getName();
                copyDirectiory(dir1, dir2);
            }
        }
    }

    /**
     * 复制文件
     *
     * @param sourceFile
     * @param targetFile
     * @throws IOException
     */
    public static void copyFile(File sourceFile, File targetFile) throws IOException {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        createFolder(targetFile);
        try {
            // 新建文件输入流并对它进行缓冲
            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));

            // 新建文件输出流并对它进行缓冲
            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
            outBuff.flush();
            // 缓冲数组
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // 刷新此缓冲的输出流
            outBuff.flush();
            // 关闭流
        } finally {
            // 关闭流
            if (inBuff != null) inBuff.close();
            if (outBuff != null) outBuff.close();
        }
    }

    public static boolean isFile(File file) {
        // if(file.exists())
        // return file.isFile();
        return isFile(file.getAbsolutePath());
    }

    public static boolean isFile(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            String separator = "";
            if (OSUtil.getOsType() == OSUtil.OSType.WINDWODS) {
                separator = "\\\\";
            } else if (OSUtil.getOsType() == OSUtil.OSType.LIUNX) {
                separator = "/";
            }
            String folders[] = filename.split(separator);
            if (folders.length > 0) {
                filename = folders[folders.length - 1];
                int dot = filename.lastIndexOf('.');
                if (dot > -1) {
                    return true;
                }
            }
        }
        return false;
    }

    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }

    // 比较两个文件夹的区别 0:不变 1:新增 2:修改 3:删除
    public static Map<String, Object> comparePulishFolder(String souceRoot, String sourcePath, String targetRoot, String targetPath) {
        try {
            File sourceFile = new File(souceRoot + sourcePath);
            File targetFile = new File(targetRoot + targetPath);
            Map<String, Object> m1 = new HashMap<String, Object>();
            compareFolderRecursion(new File(souceRoot).getAbsolutePath(), sourceFile, targetFile, m1);
            Map<String, Object> m2 = new HashMap<String, Object>();
            compareFolderRecursion(new File(targetRoot).getAbsolutePath(), targetFile, sourceFile, m2);
            for (Map.Entry<String, Object> ei : m2.entrySet()) {
                if (((int) ei.getValue()) == 1) {
                    m1.put(ei.getKey(), 3);
                } else if (((int) ei.getValue()) == 2) {
                    m1.put(ei.getKey(), 2);
                }
            }
            return m1;
        } catch (Exception e) {
            return new HashMap<String, Object>();
        }
    }

    // 0:不变 1:新增 2:修改 3:删除
    private static int compareFolderRecursion(String root, File sourceFile, File targetFile, Map<String, Object> map) throws Exception {
        int state = 0;
        // 如果tf没有，则表示新增
        if (!sourceFile.exists()) {
            state = 3;
        } else if (!targetFile.exists()) {
            state = 1;
        } else if (sourceFile.isDirectory()) {// 文件夹比较
            for (File f : sourceFile.listFiles()) {
                String fp = f.getAbsolutePath().replace(sourceFile.getAbsolutePath(), "");
                int c = compareFolderRecursion(root, f, new File(targetFile.getAbsoluteFile() + fp), map);
                if (c > 0) state = 2;
            }
        } else if (sourceFile.isFile()) {// 文件比较
            if (!targetFile.isFile()) {
                state = 2;
            } else if (!FileOperate.getMD5Flag(sourceFile).equals(FileOperate.getMD5Flag(targetFile))) {// MD5比较
                state = 2;
            }
        }
        if (state != 0) map.put(sourceFile.getAbsolutePath().replace(root, ""), state);
        return state;
    }
}
